package com.huishen.edrivenew.ui;

import java.text.NumberFormat;

import org.json.JSONObject;

import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

import com.huishen.edrivenew.AppContext;
import com.huishen.edrivenew.R;
import com.huishen.edrivenew.base.BaseActivity;
import com.huishen.edrivenew.bean.Detial3Bean;
import com.huishen.edrivenew.bean.DetialBean;
import com.huishen.edrivenew.bean.DetialInBean;
import com.huishen.edrivenew.bean.LessInfoBean;
import com.huishen.edrivenew.net.JsonResponseHandler;
import com.huishen.edrivenew.net.NetApi;
import com.huishen.edrivenew.widget.MassageListener;
import com.huishen.edrivenew.widget.MessageDialog;
import com.huishen.edrivenew.widget.MessageListDialog;

/**
 * 详情页面
 */
public class DetialActivity extends BaseActivity {
    
    @InjectView(R.id.header_title)
    public TextView title;
    @InjectView(R.id.header_back)
    public ImageButton back;
    
    @InjectView(R.id.de_date)
    public TextView date;
    @InjectView(R.id.de_coaname)
    public TextView name;
    @InjectView(R.id.de_money)
    public TextView money;
    @InjectView(R.id.de_time)
    public TextView time;
    
    @InjectView(R.id.de_title_1)
    public View title1;
    @InjectView(R.id.de_title_2)
    public View title2;
    @InjectView(R.id.de_title_3)
    public View title3;
    @InjectView(R.id.de_title_4)
    public View title4;
    @InjectView(R.id.de_title_5)
    public View title5;
    @InjectView(R.id.de_title_6)
    public View title6;
    
    
    @InjectView(R.id.btn_start)
    public Button comBtn;
    
    private int id;
    
    LessInfoBean bean;
    DetialBean detialbean;
    
    private MessageDialog mdialog;
    private MessageListDialog listDialog;
    
    private int mW;
    private int mH;
//    GraphicalView mView;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.activity_detial);
        ButterKnife.inject(this);
        
        setDate();
        request(id);
    }
    
    public void resetDate() {
        date.setText(detialbean.lessonDate);
        name.setText("选择教练：" + detialbean.coachName);
        money.setText("共计：  " + detialbean.price + "元");
        time.setText("行驶时间：" +  detialbean.time +"（小时）" + "   行驶距离：" + detialbean.distance + "（公里）");
        
        if(detialbean.subject == 2) {
            for(int i=0;i<6;i++) {
                DetialInBean km2InBean = detialbean.getkm2Bean(i);
                View v = getView(i);
                setViewIem(km2InBean, v);
            }
        } else if(detialbean.subject == 3) {
            for(int i=0;i<6;i++) {
                View v = getView(i);
                if(i>=detialbean.detial3List.size()) {
                    setViewIem(v);
                    continue;
                }
                Detial3Bean km3InBean = detialbean.detial3List.get(i);
                setViewIem(km3InBean, v);
            }
        }
        
    }
    
    private View getView(int index) {
        switch(index) {
            case 0:
                return title1;
            case 1:
                return title2;
            case 2:
                return title3;
            case 3:
                return title4;
            case 4:
                return title5;
            case 5:
                return title6;
        }
        return null;
    }
    
    private void setViewIem(final DetialInBean km2InBean, View v) {
        if(v == null) {
            return;
        }
        if(km2InBean == null) {
            v.setVisibility(View.GONE);
            return;
        }
        v.setVisibility(View.VISIBLE);
        
        TextView tName = (TextView) v.findViewById(R.id.di_name);
        TextView tcount= (TextView) v.findViewById(R.id.di_count);
        TextView tpass = (TextView) v.findViewById(R.id.di_pass);
        TextView tDetial = (TextView) v.findViewById(R.id.di_detial);
//        ImageView line = (ImageView) v.findViewById(R.id.line);
        
        tName.setText(km2InBean.name);
        tcount.setText(km2InBean.count+"");
        tpass.setText(km2InBean.pass + "%");
        tDetial.setText("查看详情");
        
        if(km2InBean.points != null && km2InBean.points.size()!=0) {
            tDetial.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
            tDetial.setTextColor(AppContext.getInstance().getResources().getColor(R.color.main_color));
            tDetial.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 点击弹出详情框
                    listDialog = new MessageListDialog(DetialActivity.this, km2InBean.name, km2InBean.points);
                    listDialog.show();
                }
            });
        }
    }
    
    private void setViewIem(final Detial3Bean km3InBean, View v) {
        if(v == null) {
            return;
        }
        if(km3InBean == null) {
            v.setVisibility(View.GONE);
            return;
        }
        v.setVisibility(View.VISIBLE);
        
        TextView tName = (TextView) v.findViewById(R.id.di_name);
        TextView tcount= (TextView) v.findViewById(R.id.di_count);
        TextView tpass = (TextView) v.findViewById(R.id.di_pass);
        TextView tDetial = (TextView) v.findViewById(R.id.di_detial);
        
        tName.setText(km3InBean.name);
        tcount.setText(km3InBean.scene+"");
        
        tpass.setText(   computer(km3InBean.scene_pass, km3InBean.scene) + "%"  );
        tDetial.setText("查看详情");
        
        if(km3InBean.points != null && km3InBean.points.size()!=0) {
            tDetial.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
            tDetial.setTextColor(AppContext.getInstance().getResources().getColor(R.color.main_color));
            tDetial.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 点击弹出详情框
                    listDialog = new MessageListDialog(DetialActivity.this, km3InBean.name, km3InBean.points);
                    listDialog.show();
                }
            });
        }
    }
    private void setViewIem(View v) {
        v.setVisibility(View.GONE);
    }
    
    
    private String computer(int num1, int num2) {
        NumberFormat numberFormat = NumberFormat.getInstance();
        numberFormat.setMaximumFractionDigits(2);
        return numberFormat.format((float)num1/(float)num2 * 100);
    }
    
    
    private void setDate() {
        title.setText("培训详情");
        bean = AppContext.getInstance().judgeBean;
        if(bean == null) {
            return;
        }
        id = bean.lessonInfoId;

    }
    
    @OnClick(R.id.btn_start)
    public void login() {
        toMain();
    }
    
    
//    private void showMegDialog() {
//        mdialog = new MessageDialog(this, "", "你已成功提交评价，我们会根据评价扣除学时，让你离考试更近一步", false,
//            new MassageListener() {
//                @Override
//                public void setCommitClick() {
//                       toMain();
//                }
//                @Override
//                public void setCancelClick() {
//                }
//            });
//        mdialog.setCancelable(false);
//        mdialog.setCancelHide();
//        mdialog.show();
//    }
    
    private void request(int id) {
        showDialog();
        NetApi.queryStuTrainDetail(this, new JsonResponseHandler() {
            @Override
            public void onSuccess(int code, JSONObject jsonObj, Object[] args) {
                detialbean = (DetialBean) DetialBean.parseJson(jsonObj);
                if(detialbean.code == 0) {
                    resetDate();
                } else {
                    showToast("获取数据失败！");
                }
                dismissDialog();
            }
            @Override
            public void onFailure(int code, String errorMessage, Object[] args) {
                dismissDialog();
                showToast("获取数据失败！");
            }
            @Override
            public void onSessionFailure() {
                dismissDialog();
                super.onSessionFailure();
            }
        }, id+"");
    }
    
    // 画表格图
//    public void setChatView() {
//        GraphicalView mView = (GraphicalView) findViewById(R.id.result_main_view);
//        DisplayMetrics met = AppContext.getInstance().getScreenInfo(this);
//        mH = met.heightPixels;
//        mW = met.widthPixels;
//        
//        LayoutParams param = mView.getLayoutParams();
//        param.width = mW *2/3;
//        param.height = mH *2/5;
//        
//        mView.setLayoutParams(param);
////        XYChart chart = executeMain();
//    }
    
    
    
    @OnClick(R.id.header_back)
    public void toMain() {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        this.startActivity(intent);
        this.finish();
    }
    
}
